"""Core game classes."""

__author__ = 'Josh Oosterman'

import pygame
import pygame.time
import sys

from potv import input

SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
TARGET_FPS = 60
DEBUG = False


class Game(object):
  """The main game class."""

  def __init__(self):
    pygame.init()
    self.screen = pygame.display.set_mode(SCREEN_SIZE)

  def Run(self, level):
    """Run the game, one frame at a time."""
    target_ticks = 1000.0 / TARGET_FPS
    global DEBUG
    while True:
      start_frame_ticks = pygame.time.get_ticks()
      for event in pygame.event.get():
        if event.type == pygame.QUIT:
          break
      input.Update()
      if input.WasPressed(input.QUIT_GAME):
        break
      if input.WasPressed(input.TOGGLE_DEBUG):
        DEBUG = not DEBUG
      level.Step()
      level.Draw(self.screen)
      pygame.display.flip()
      frame_ticks = pygame.time.get_ticks() - start_frame_ticks
      wait = int(target_ticks - frame_ticks)
      if wait:
        pygame.time.delay(wait)
      self.fps = TARGET_FPS if wait else 1000.0 / frame_ticks
    pygame.quit()

